<template>
  <div class="LoginBody">
    <div class="formBody">
      <div class="avater">
        <div class="avater_img">
          <img src="../assets/logo.png" alt="" />
        </div>
      </div>
      <div class="formContainer">
        <el-form
          ref="form"
          :model="loginForm"
          class="form_com"
          :rules="formRules"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-s-custom"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="el-icon-lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="login('form')">登录</el-button>
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      formRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '字数在3-5之间', trigger: 'blur' },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
          {
            min: 5,
            max: 10,
            message: '字数在5-10之间',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    login(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let { data: res } = await this.$http.post('login', this.loginForm)
          if (res.meta.status !== 200) {
            this.$message.error('登录失败')
          } else {
            this.$message.success('登录成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          }
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.LoginBody {
  height: 100vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .formBody {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #fff;
    position: relative;
    .avater {
      width: 152px;
      height: 152px;
      box-sizing: border-box;
      padding: 10px;
      border-radius: 50%;
      background-color: #fff;
      position: absolute;
      left: 50%;
      top: calc(-125px / 2);
      transform: translateX(-50%);
      box-shadow: 0 0 10px #ddd;
      .avater_img {
        margin-top: -2px;
        background-color: #eee;
        border-radius: 50%;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .formContainer {
      width: 100%;
      box-sizing: border-box;
      padding: 0 20px;
      position: absolute;
      bottom: 0;
      .form_com {
        width: 100%;
        .btn {
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
}
</style>
